/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.spestimator.files.fasta.digest;

import com.compomics.dbtoolkit.toolkit.EnzymeDigest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class FastaDigester {

    private static final String DBTOOLKIT_CLASS_NAME = "com.compomics.dbtoolkit.toolkit.EnzymeDigest";
    private static final Logger LOGGER = Logger.getLogger(FastaDigester.class);
    private static final File enzymeFile = null;
    private static int missed_cleavages = 2;
    private static final String enzymeName = "Trypsin";

    public static void digestWithEnzyme(File inputFile, File outputFile) {
        // Usage:\n\tEnzymeDigest [--enzymeFile <custom_enzyme_file>] [--enzyme <enzymeName> [--mc <number_of_missed_cleavages>]] [--filter <filter_name> [--filterParam \"<filter_parameter>\"]] [--lowMass <lower_mass_treshold> --highMass <higher_mass_treshold>] --input <input_db_name> <output_db_name>\n\n\tNote that an existing output file will be silently overwritten!
        LOGGER.info("Digesting merged fastafile using dbToolkit : ");
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add("--input");
        arguments.add(inputFile.getAbsolutePath());
        arguments.add("--mc");
        arguments.add(String.valueOf(missed_cleavages));
        arguments.add("--enzyme");
        arguments.add(enzymeName);
        arguments.add(outputFile.getAbsolutePath());
        System.out.println(arguments.toString().replace(",", " "));
        String[] argumentsAsArray = new String[arguments.size()];
        EnzymeDigest.main(arguments.toArray(argumentsAsArray));
        LOGGER.info("Merged fastafiles...");
    }

    public static void main(String[] args) {
        try {
            File input = new File(FastaDigester.class.getResource("/output.FASTA").toURI());
            File output = new File(FastaDigester.class.getResource("/output_digested.FASTA").toURI());
            digestWithEnzyme(input, output);
            System.out.println(output.getAbsolutePath());
            try (InputStream in = new FileInputStream(output);
                    BufferedReader reader =
                    new BufferedReader(new InputStreamReader(in))) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException x) {
                System.err.println(x);
            }
        } catch (URISyntaxException ex) {
            java.util.logging.Logger.getLogger(FastaDigester.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
